﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FB.DataAccess;
using FB.Bussiness;
using FB.Utilities;

namespace FB.admin
{
    public partial class PlaylistEdit : System.Web.UI.Page
    {
        /// <summary>
        ///  View State EditItemID
        /// </summary>
        private Guid? EditItemID
        {
            get
            {
                object obj = this.ViewState["_EditItemID"];
                if (obj == null)
                    return null; //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_EditItemID"] = value;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadForm();
            }
        }
        private void CheckPermission()
        {
            if (Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR)
                || Page.User.IsInRole(Constants.ROLE_AUTHOR)
                || Page.User.IsInRole(Constants.ROLE_CONTRIBUTER)
                || Page.User.IsInRole(Constants.ROLE_EDITOR))
            {
                //
            }
            else
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        private void LoadForm()
        {
            Guid TempID;
            if (Guid.TryParse(Request.QueryString["ID"], out TempID))
            {
                EditItemID = TempID;
            }
            LoadFormEdit();
        }

        private void LoadVideoCategory(Guid videoID)
        {
            VideoCategoryBO BO = new VideoCategoryBO();
            chklVideoCategory.DataSource = BO.GetAllVideoCategory();
            chklVideoCategory.DataTextField = "Name";
            chklVideoCategory.DataValueField = "ID";
            chklVideoCategory.DataBind();
            Video_CategoryBO RelationBO = new Video_CategoryBO();
            List<Guid> cateSelectedList = RelationBO.GetCategoryIDOfVideo(videoID);
            foreach (ListItem item in chklVideoCategory.Items)
            {
                if (cateSelectedList.Contains(Guid.Parse(item.Value)))
                {
                    item.Selected = true;
                }
            }
        }

        private void LoadFormEdit()
        {
            lblTitle.Text = "Playlist: Edit";
            VideoBO BO = new VideoBO();
            Video CurrentVideo = BO.GetVideoById((Guid)EditItemID);
            if (CurrentVideo != null)
            {
                txtTitle.Text = (CurrentVideo.Title != null) ? CurrentVideo.Title : CurrentVideo.UTitle;
                txtDescription.Text = (CurrentVideo.Description != null) ? CurrentVideo.Description : CurrentVideo.UDescription;
                //TODO: Update tags
                txtTags.Text = (CurrentVideo.Title != null) ? CurrentVideo.Title : CurrentVideo.UTags;
                chkEnable.Checked = (bool)CurrentVideo.Enable;
                LoadVideoCategory(CurrentVideo.ID);
                //Load Youtube properties
                txtYTitle.Text = CurrentVideo.UTitle;
                txtYAuthor.Text = CurrentVideo.Author;
                txtYChanel.Text = Convert.ToString(CurrentVideo.Chanel.DisplayName);
                txtYDescription.Text = CurrentVideo.UDescription;
                txtYDisliked.Text = Convert.ToString(CurrentVideo.UDisliked);
                txtYDuration.Text = Convert.ToString(CurrentVideo.Duration);
                txtYLiked.Text = Convert.ToString(CurrentVideo.ULiked);
                txtYRatingAverange.Text = Convert.ToString(CurrentVideo.URatingAverage);
                txtYTags.Text = CurrentVideo.UTags;
                txtYVideoID.Text = CurrentVideo.YoutubeID;
                txtYViewCount.Text = Convert.ToString(CurrentVideo.UViewCount);
                imgThumbail.ImageUrl = Utilities.Common.GetYoutubeThumb(CurrentVideo.YoutubeID);
            }
            else
            {
                Response.Redirect("PlaylistList.aspx");
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveForm();
            //TODO: Show msg
        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveForm();
            Response.Redirect("PlaylistList.aspx");
        }
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            SaveForm();
            EditItemID = null;
            Response.Redirect("PlaylistAdd.aspx?ID=0");
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("PlaylistList.aspx");
        }
        private void SaveForm()
        {
            VideoBO BO = new VideoBO();
            Video EditVideo = BO.GetVideoById((Guid)EditItemID);
            EditVideo.Title = txtTitle.Text;
            EditVideo.Description = txtDescription.Text;
            EditVideo.Enable = chkEnable.Checked;
            //EditVideo.ThumbnailUrl = imgThumbail.ImageUrl;
            BO.UpdateVideo(EditVideo);
            
            //Update category of this video
            List<Guid> NewCatList = new List<Guid>();
            foreach (ListItem item in chklVideoCategory.Items)
            {
                if (item.Selected)
                {
                    NewCatList.Add(Guid.Parse(item.Value));
                }
            }
            Video_CategoryBO CBO = new Video_CategoryBO();
            CBO.UpdateCategoryOfVideo((Guid)EditItemID, NewCatList);

            //TODO: Update Tags for Video

        }
    }
}